/**

 @Name：layuiAdmin 公共业务
 @Author：贤心
 @Site：http://www.layui.com/admin/
 @License：LPPL
    
 */
 
layui.define(['admin', 'table', 'util','upload','form','laydate'],function(exports){
  var $ = layui.$
  ,layer = layui.layer
  ,laytpl = layui.laytpl
  ,setter = layui.setter
  ,view = layui.view
  ,admin = layui.admin
  ,form = layui.form
  ,table = layui.table;
  
  //公共业务的逻辑处理可以写在此处，切换任何页面都会执行
  //……
 
	form.verify({
			 price:[/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,"请输入合法的金额"],
			 englishLetter:[/^[A-Za-z]+$/,"只能输入英文字母"]
	});
  //日期，时间，分钟样式绑定
	var laydate = layui.laydate;  
	lay('.date').each(function(){
		laydate.render({
			elem: this,
			type:"date",
			trigger: 'click'
		});
	});  
	lay('.datetime').each(function(){
		laydate.render({
			elem: this,
			type:"datetime",
			trigger: 'click'
		});
	});
	lay('.time').each(function(){
		laydate.render({
			elem: this,
			type:"time",
			trigger: 'click'
		});
	}); 	  	

  //退出
  admin.events.logout = function(obj){
    //执行退出接口
	var uri=obj.attr("uri");
	var loginUrl=obj.attr("loginUrl");
    admin.req({
      url: uri
      ,type: 'get'
      ,data: {}
      ,done: function(res){ //这里要说明一下：done 是只有 response 的 code 正常才会执行。而 succese 则是只要 http 为 200 就会执行
        //清空本地记录的 token，并跳转到登入页
        admin.exit(function(){
          location.href = loginUrl;
        });
      }
    });
  };

  
  //对外暴露的接口
  exports('common', {});
  
  
  //打开弹窗并验证是否有权限接口
  exports('layerOpen', function(uri,title){
	  			var width=typeof(width)==='undefined'?'100%':width;
				var height=typeof(height)==='undefined'?'100%':height;
				var title=typeof(title)==='undefined'?'信息':title;
				if (uri.indexOf("?") == -1){
					var uri=uri+"?random="+Math.random(); //加上随机数，避免304缓存
				}else{
					var uri=uri+"&random="+Math.random(); //加上随机数，避免304缓存
				}
				var tableLoadDiv = layer.load(1,{shade:[0.5, '#000']}); //加载层,table.render完成时需要关闭
				var index = layer.open({
											type: 2,
											title:title,
											content: uri,
											area: ['1200px','800px'],
											anim:5,
											shadeClose:false,
											//offset: ['-100px', '-200px'],
											maxmin: true,
											success: function(layero, index){
												var body = layer.getChildFrame('body', index);
												var iframeWin = window[layero.find('iframe')[0]['name']];
												var html=body.html();
												try{
													var jsonObj = $.parseJSON(html.replace(/<.*?>/ig,""));
													window.top.layer.msg(jsonObj.msg, {icon: 2, time: 1000,shade:[0.5,'#000']});
													layer.close(index);//关闭当前弹出层
													layer.close(tableLoadDiv);
													return;
												}catch(e){		
													layer.full(index,{offset: 'auto'});									
													layer.iframeAuto(index);
													layer.close(tableLoadDiv);
												}
											}									
										});
  });
  //批量删除
  exports('deleteAll', function(uri,msg,listTableObj){
				var checkStatus = table.checkStatus('list'),data = checkStatus.data; //获得选中的数据
                if(data.length === 0) return window.top.layer.msg('请选择要删除的数据',{icon:2,time: 1000,shade:[0.5,'#000']});
                var ids = [];
                $.each(data, function(i,val){
                    ids[i] = val['id'];
                });
                window.top.layer.confirm(msg, {icon: 3, title:'操作提示'},function(){
                    admin.req({
                        url: uri,
						type: 'post',
						data: {ids:ids},
						success: function (res) {
                            layer.closeAll('dialog');
                            if(res.code==0){
                                	window.top.layer.msg(res.msg, {icon: 1, time: 1000,shade:[0.5,'#000']}, function () {
                                    listTableObj.reload();
                                });
                            }else if (res.code != 0) {
									window.top.layer.msg(res.msg, {icon: 2, time: 1000,shade:[0.5,'#000']});
									return;
							}
                        }
                    });
                });
  }); 
  
  //删除接口
  exports('doDelete', function(uri,msg,id,obj){
					window.top.layer.confirm(msg, {icon: 3, title:'操作提示'},function () {
							admin.req({
								url: uri, 
								type: 'post', 
								data: {'id': id}, 
								success: function (res) {
									layer.closeAll('dialog');
									if (res.code == 0) {
										window.top.layer.msg(res.msg, {icon: 1, time: 1000,shade:[0.5,'#000']}, function () {
											obj.del();
										});
									}else{
										window.top.layer.msg(res.msg, {icon: 2, time: 1000,shade:[0.5,'#000']});
										return;
									}
								}
							});
					});	  
  });

  // 确认提示并操作
	exports('doSomething', function(uri,msg,id,listTableObj){
		window.top.layer.confirm(msg, {icon: 3, title:'操作提示'},function () {
			admin.req({
				url: uri,
				type: 'post',
				data: {'id': id},
				success: function (res) {
					layer.closeAll('dialog');
					if (res.code == 0) {
						window.top.layer.msg(res.msg, {icon: 1, time: 1000,shade:[0.5,'#000']}, function () {
							listTableObj.reload();
						});
					}else{
						window.top.layer.msg(res.msg, {icon: 2, time: 1000,shade:[0.5,'#000']});
						return;
					}
				}
			});
		});
	});


});